package com.example.tao.smartcar.bluetooth;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.media.Image;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.example.tao.smartcar.view.indicator.HalfIndicatorView;
import com.example.tao.smartcar.view.indicator.IndicatorView;
import com.example.tao.smartcar.view.valuecontrolview.ValueChangeListener;
import com.example.tao.smartcar.R;
import com.example.tao.smartcar.utils.BluetoothService;
import com.example.tao.smartcar.view.valuecontrolview.ValueControlView;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TouchControlActivity extends Activity implements ValueChangeListener {

    private static String TAG = "TouchControlActivity";

    private ValueControlView speedControlView, directionControlView;
    private ImageView mWheelImgview;
    private IndicatorView speedIndicatorView, energyIndicatorView;
    private HalfIndicatorView directionIndicatorView;

    private boolean isSpeedChanged = false, isDirectionChanged = false;

    private Handler mHandler;

    private BluetoothService mBluetoothService = null;

    private Matrix mMatrix = new Matrix();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_touch_control);

        initView();

        mBluetoothService = OperationSettingActivity.getBluetoorhService();

        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                Integer wr = Integer.valueOf(msg.what);
                mBluetoothService.write(wr.byteValue());
            }
        };

        speedControlView.setOnValueChangeListener(this);
        directionControlView.setOnValueChangeListener(this);

    }

    private void initView() {
        speedControlView = (ValueControlView) this.findViewById(R.id.speed_controlview);
        directionControlView = (ValueControlView) this.findViewById(R.id.direction_controlview);
        //mWheelImgview = (ImageView) this.findViewById(R.id.wheel);
        speedIndicatorView = (IndicatorView) this.findViewById(R.id.speed_indicator);
        energyIndicatorView = (IndicatorView) this.findViewById(R.id.energy_indicator);
        directionIndicatorView = (HalfIndicatorView) this.findViewById(R.id.direction_indicator);

        //Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(R.mipmap.wheel)).getBitmap();
        //mWheelImgview.setImageBitmap(bitmap);

        speedControlView.setValueHold(true);
        speedControlView.setPosSensitivity((byte)10);
        speedControlView.setNegSensitivity((byte)(-10));
        directionControlView.setPosSensitivity((byte)5);
        directionControlView.setNegSensitivity((byte)(-5));

        speedIndicatorView.setMax(40);
        speedIndicatorView.setMin(-40);
        speedIndicatorView.setValue(0);

        energyIndicatorView.setMax(100);
        energyIndicatorView.setMin(0);
        energyIndicatorView.setValue(90);

        directionIndicatorView.setMax(40);
        directionIndicatorView.setMin(-40);
        directionIndicatorView.setValue(0);
    }

    @Override
    public void onValueChanged(View view) {

        if (view == directionControlView) {
            //Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(R.mipmap.wheel)).getBitmap();
            //float degree = 2*(float)(directionControlView.getValue());
            //mMatrix.setRotate(degree);
            //bitmap = bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mMatrix, true);
            //mWheelImgview.setImageBitmap(bitmap);
            int direction = directionControlView.getValue();
            directionIndicatorView.setValue(direction);
        }

        if (view == speedControlView) {
            int speed = speedControlView.getValue();
            speedIndicatorView.setValue(speed);
        }

        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(1);
        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                byte speed = speedControlView.getValue();
                byte direction = directionControlView.getValue();
                Message speed_command = mHandler.obtainMessage();
                speed_command.what = 0x7F;
                mHandler.sendMessage(speed_command);
                Message speed_data = mHandler.obtainMessage();
                speed_data.what = speed;
                mHandler.sendMessage(speed_data);
                Message direction_command = mHandler.obtainMessage();
                direction_command.what = 0x7E;
                mHandler.sendMessage(direction_command);
                Message direction_data = mHandler.obtainMessage();
                direction_data.what = direction;
                mHandler.sendMessage(direction_data);
                Log.d(TAG, "Send speed:" + speed_data.what);
                Log.d(TAG, "Send direction:" + direction_data.what);
                try {
                    Thread.sleep(200);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        });

    }
}
